package cn.leyaber.hrm.controller;






import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;


//ES的API
@RequestMapping("/es")
@RestController
public class ElasticSearchController {

//   @Autowired
//    private CourseElasticsearchRepository courseElasticsearchRepository;
//
//    //保存课程
//    @RequestMapping(value = "/save/course",method = RequestMethod.POST)
//    public AjaxResult saveCourse(@RequestBody CourseDoc courseDoc){
//        courseElasticsearchRepository.save(courseDoc);
//        return AjaxResult.me();
//    }
//    //查询课程
//    @PreAuthorize("hasAnyAuthority('es:queryCourse')")
//    @RequestMapping(value = "/queryCourse",method = RequestMethod.POST)
//    public AjaxResult queryCourse(@RequestBody CourseQuery courseQuery){
//        //1.把courseQeury转成ES的查询对象
//        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
//        //排序
//        String sortField = courseQuery.getSortField();
//        if (StringUtils.isNotBlank(sortField)){
//            //排序的字段=========================
//            String sortFiledName = "price";
//            switch (sortField.toLowerCase()){
//                case "xl": sortFiledName = "saleCount";break;
//                case "xp": sortFiledName = "onlineTime";break;
//                case "pl": sortFiledName = "commentCount";break;
//            }
//
//            //排序方式 desc  -> SortOrder.DESC ，asc  -> SortOrder.ASC
//            String sortType = courseQuery.getSortType();
//
//            SortOrder sortOrder = SortOrder.ASC;
//
//            sortOrder = StringUtils.isNotBlank(sortType)
//                    && sortType.toLowerCase().equals("desc") ? SortOrder.DESC : SortOrder.ASC;
//
//            builder.withSort(new FieldSortBuilder(sortFiledName).order(sortOrder));
//        }
//        //分页
//        builder.withPageable(PageRequest.of(courseQuery.getPage()-1,courseQuery.getRows()));
//
//        //boolean组合查询
//        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//        if(StringUtils.isNotBlank(courseQuery.getKeyword())){
//            //DSL查询 ： keyword -> must -> matchQuery
//            boolQueryBuilder.must(QueryBuilders.matchQuery("searchField",courseQuery.getKeyword()));
//        }
//        if(null != courseQuery.getProductType()){
//            //DSL过滤 : productType -> term
//            boolQueryBuilder.filter(QueryBuilders.termQuery("courseTypeId",courseQuery.getProductType()));
//        }
//        if(null != courseQuery.getPriceMin()){
//            //DSL过滤 : priceMax;priceMin -> range
//            boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(courseQuery.getPriceMin()));
//        }
//        if(null != courseQuery.getPriceMax()){
//            //DSL过滤 : priceMax;priceMin -> range
//            boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").lte(courseQuery.getPriceMax()));
//        }
//        builder.withQuery(boolQueryBuilder);
//        //==================================================================
//        //2.调用courseElasticsearchRepository进行查询
//        Page<CourseDoc> page = courseElasticsearchRepository.search(builder.build());
//
//        //3.把结果封装成 PageList<CourseDoc> ，设置给 AjaxResult 返回
//        return AjaxResult.me().setResultObj(
//                new PageList<CourseDoc>(page.getTotalElements(),page.getContent()));
//    }
}
